<template>
  <!-- 用了一个行列布局 -->
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
    <el-col>
      <strong v-if="isRoot">   {{ treeNode.name }}</strong>
      <span v-else> {{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="operateDepts">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>

</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async operateDepts(type) {
      // command属性可以判断点击的是那个
      // console.log(type)
      try {
        if (type === 'add') {
        // 添加
        // 这里是树型组件，自己没法弹窗，需要通知父页面
        // 同时，新增时，当前部门应该作为新增的pid
        // 所以将数据一并传出方便处理
          this.$emit('addDepts', this.treeNode)
        } else if (type === 'edit') {
        // 编辑
          this.$emit('editDepts', this.treeNode)
        } else if (type === 'del') {
        // 删除
        // 先询问是否确认
          await this.$confirm('是否确认删除该部门')
          // 拿到id发请求即可
          await delDepartments(this.treeNode.id)
          // 如果正常删除完毕, 应该重新载入页面,
          // 需要通知父页面
          this.$emit('delDepts')
          this.$message.success('删除成功')
        }
      } catch (error) {
        console.log(error)
      }
    }
  }

}
</script>

<style>

</style>
